using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Text;

namespace Common
{
    /// <summary>
    /// 
    /// </summary>
    public class ResourceUtil
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="assembly"></param>
        /// <param name="resourceName"></param>
        /// <returns></returns>
        public static Image GetImageFromResource(Assembly assembly, string resourceName)
        {
            Image img = null;
            string[] resourceNames = assembly.GetManifestResourceNames();
            foreach(string resName in resourceNames)
            {
                string resourceNameWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(resName);
                if (resourceNameWithoutExtension .ToLower()== resourceName.ToLower())
                {
                    Stream imgStream = assembly.GetManifestResourceStream(resName);
                    if(imgStream !=null)
                    {
                        img = Image.FromStream(imgStream);
                        imgStream.Close();
                    }
                    break;
                }
            }
            return img;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="assembly"></param>
        /// <param name="resourceName"></param>
        /// <returns></returns>
        public static StreamReader GetEmbeddedFileContent(Assembly assembly, string resourceName)
        {
            string[] resourceNames = assembly.GetManifestResourceNames();
            foreach (string resName in resourceNames)
            {
                if (resName.ToLower() == resourceName.ToLower())
                {
                    Stream fileStream = assembly.GetManifestResourceStream(resName);
                    if (fileStream != null)
                    {
                        return new StreamReader(fileStream);
                    }
                }
            }

            return null;
        }
    }
}
